package com.lyl.xxx.modules.framework.security.handler;

import com.lyl.common.result.BaseCode;
import com.lyl.common.result.Result;
import com.lyl.xxx.common.utils.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.Luo
 * @date 2022-09-19 11:36
 * @desc 权限拒绝处理器
 */
@Slf4j
@Component
public class AccessDefinedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setStatus(BaseCode.ACCESS_DENIED.getCode());
        ServletUtil.write(response, Result.build(BaseCode.ACCESS_DENIED, e.getMessage()));
    }
}
